package cn.main.controller;

import cn.main.core.SessionHelper;
import cn.main.core.UserSession;
import cn.main.entity.sys.SysColumn;
import cn.main.service.ColumnService;
import com.mysql.jdbc.StringUtils;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Author: chengwei
 * Date:   2016/5/27
 * Time:   17:20
 */
@Controller
@RequestMapping("/user/column")
public class ColumnController {
    
    @Resource 
    private ColumnService service;

    @RequestMapping("/co")
    public ModelAndView getShowColumn(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取栏目
        UserSession session = SessionHelper.getUserSession(req);
        if (session == null) {
            return new ModelAndView("/home");
        }
//        Map<String,Object> map = service.getColumn(session.getUser(),req.getParameter("column_id"));
        return new ModelAndView("/manager/home/columnList");
    }

    @RequestMapping("/col/one")
    public ModelAndView getColumnList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取栏目
        UserSession session = SessionHelper.getUserSession(req);
        if (session == null) {
            return new ModelAndView("/home");
        }
        Map<String,Object> map = service.getColumn(session.getUser(),req.getParameter("column_id"));
        return new ModelAndView("/manager/home/columnList",map);
    }

    @RequestMapping("/col")
    @ResponseBody
    public String getColumn(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        //获取构架 
        UserSession session = SessionHelper.getUserSession(req);
        if (session == null) {
            return "{}";
        }
        Map<String,Object> map = service.getColumn(session.getUser());
        return JSONObject.fromObject(map).toString();
    }
    
    @RequestMapping("/save")
    @ResponseBody
    public String saveOrUpdateColumn(SysColumn column,HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        service.saveOrUpdate(session,column);
        return null;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String deleteColumn(@RequestParam("column_id") String columnId,HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        service.deleteColumn(session,columnId);
        return null;
    }

    @RequestMapping("/find")
    @ResponseBody
    public SysColumn findColumnById(@RequestParam("column_id") String columnId,HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        return service.findColumnById(session,columnId);
    }

    @RequestMapping("/list")
    @ResponseBody
    public List<SysColumn> getColumnList(HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        return service.getColumnList(session);
    }

    @RequestMapping("/showb")
    @ResponseBody
    public List<SysColumn> getColumnListb(HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        return service.getColumnList();
    }

    @RequestMapping("/show")
    public ModelAndView showColumnList(HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        List<SysColumn> columns = service.getColumnList();
        return new ModelAndView("/sys/column/columnList","columns",columns);
    }

    @RequestMapping("/form")
    public ModelAndView formColumnById(HttpServletRequest request) throws Exception {
        UserSession session = SessionHelper.getUserSession(request);
        String columnId = request.getParameter("column_id");
        SysColumn column;
        if (!StringUtils.isNullOrEmpty(columnId)) {
            column = service.findColumnById(session,columnId);
        } else {
            column = new SysColumn();
        }
        
        return new ModelAndView("/sys/column/formColumn","column",column);
    }
}
